package com.ceba.test.modules.regist.dao;

import java.util.Date;
import java.util.Map;

import com.ceba.base.web.dao.IBaseDao;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.modules.regist.entity.EnterpriseRegist;

import abc.ID;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;

public class EnterpriseRegistDao extends IBaseDao{

	/**
	 * 添加一条记录，默认试用日期从激活当日起3个月,签名份数3000
	 * @return
	 */
	public Boolean add(EnterpriseRegist enterprise) throws Exception{
		IStore store = StoreManager.create("g_enterprise_regist");
		IObject object = new XObject("g_enterprise_regist");
		object.put("ID",ID.getGuid());
		object.put("ENTERPRISE_NAME",enterprise.getEnterpriseName());
		object.put("AVALIABLE_DATE",enterprise.getAvailableDate());
		object.put("SIGN_COUNT",enterprise.getSignCount());
		object.put("NEW_AVALIABLE_DATE",enterprise.getNewAvailableDate());
		object.put("NEW_SIGN_COUNT",enterprise.getNewSignCount());
		object.put("SIGNED_COUNT",enterprise.getSignedCount());
		object.put("UPDATE_FLAG",enterprise.getUpdateFlag());
		object.put("VERSION",enterprise.getVersion());
		object.put("CREATE_TIME",enterprise.getCreateTime());
		object.put("SYSTEM_TYPE", enterprise.getSystemType());
		object.put("FORMAL_USE_TIME", enterprise.getFormalUseTime());
		object.put("COLLECT_SQL_FLAG", enterprise.getCollectionSqlFlag());
		object.put("ALREADY_ACTIVE_TIMES", enterprise.getAlreadyActiveTimes());
		object.put("ACTIVE_TIMES", enterprise.getActiveTimes());
		object.put("FEE_TYPE", enterprise.getFeeType());
		object.setCreateTime();
		object.setCreateUser();
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}

	public DataTable getEnterpriseRegistByName(String enterpriseName)throws Exception {
		Sql sql = new Sql("SELECT * FROM g_enterprise_regist WHERE ENTERPRISE_NAME=?");
		sql.addParameter(enterpriseName);
		DataTable dt = SqlSessionManager.getDataTable(sql);
		return dt;
	}



	/**
	 * 删除一条数据
	 * @param id 数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception{
		
		Sql sql = new Sql("DELETE FROM g_enterprise_regist WHERE ID = ?");
		sql.addParameter(id);
		return SqlSessionManager.execute(sql);
	}
	
	public Boolean deleteByName(String enterpriseName) throws Exception{
		
		Sql sql = new Sql("DELETE FROM g_enterprise_regist WHERE ENTERPRISE_NAME = '"+enterpriseName+"'");
		
		if(SqlSessionManager.execute(sql)){
			sql = new Sql("DELETE FROM enterprise WHERE ENTERPRISE_NAME = '"+enterpriseName+"'");
		}
		return SqlSessionManager.execute(sql);
	}
	
	public Boolean updateByName(String enterpriseName,String version,String signedCount, String alreadyActiveTimes) throws Exception{
		Sql sql =null;
		if(StringUtil.isEmptyString(version)){
			sql = new Sql("UPDATE g_enterprise_regist SET UPDATE_FLAG = '1',"
					+ "SIGNED_COUNT = '"+signedCount+"',ALREADY_ACTIVE_TIMES='"+alreadyActiveTimes+"' "
					+ " WHERE ENTERPRISE_NAME = '"+enterpriseName+"'");
		}else{
			sql = new Sql("UPDATE g_enterprise_regist SET UPDATE_FLAG = '1',"
					+ "VERSION='"+version+"',SIGNED_COUNT = '"+signedCount+"',ALREADY_ACTIVE_TIMES='"+alreadyActiveTimes+"' "
					+ " WHERE ENTERPRISE_NAME = '"+enterpriseName+"'");
		}
		
		return SqlSessionManager.execute(sql);
	}
	
	public Boolean updateServer(EnterpriseRegist enterpriseRegist) 
					throws Exception{
		Sql sql =null;
		String updateString = "UPDATE g_enterprise_regist SET UPDATE_FLAG = '1',"
				+"ENTERPRISE_NAME='"+enterpriseRegist.getEnterpriseName()+"',"
				+ "SIGN_COUNT = '"+enterpriseRegist.getSignCount()+"',AVALIABLE_DATE='"+enterpriseRegist.getAvailableDate()+"',"
				+"NEW_AVALIABLE_DATE='"+enterpriseRegist.getNewAvailableDate()+"',NEW_SIGN_COUNT='"+enterpriseRegist.getNewSignCount()+"',"
				+"UPDATE_FLAG='"+enterpriseRegist.getUpdateFlag()+"',SYSTEM_TYPE='"+enterpriseRegist.getSystemType()+"',"
				+"FEE_TYPE='"+enterpriseRegist.getFeeType()+"',"
				+"COLLECT_SQL_FLAG='"+enterpriseRegist.getCollectionSqlFlag()+"',"
				+"ACTIVE_TIMES='"+enterpriseRegist.getActiveTimes()+"'";
		if(!StringUtil.isEmptyString(enterpriseRegist.getFormalUseTime())){
			updateString = updateString +",FORMAL_USE_TIME='"+enterpriseRegist.getFormalUseTime()+"'";
		}
		updateString = updateString+" WHERE ID = '"+enterpriseRegist.getId()+"'";
		sql = new Sql(updateString);
		
		return SqlSessionManager.execute(sql);
	}

	public boolean updateFormalUseTimeByName(String enterpriseName,String time) {
		Sql sql = new Sql("UPDATE g_enterprise_regist SET FORMAL_USE_TIME='"+time
				+"' WHERE ENTERPRISE_NAME = '"+enterpriseName+"'");
		
		return SqlSessionManager.execute(sql);
	}


	public DataTable queryEnterpriseRegist(String key, Map<String, Object> dic) {
		SqlDataSource dataSource = new SqlDataSource();
		Sql sql = null;
		if(!StringUtil.isEmptyString(key)){
			  sql = new Sql(
					"SELECT ID,ENTERPRISE_NAME,AVALIABLE_DATE,SIGN_COUNT,CREATE_TIME,NEW_AVALIABLE_DATE,"
					+ " NEW_SIGN_COUNT,SIGNED_COUNT,UPDATE_FLAG,SYSTEM_TYPE,FEE_TYPE,"							
					+ " ALREADY_ACTIVE_TIMES,ACTIVE_TIMES,FORMAL_USE_TIME,VERSION "
					+ " FROM g_enterprise_regist "
					+ " where ENTERPRISE_NAME like '%"+key+"%' order by CREATE_TIME desc "
					);
			}else{
			  sql = new Sql(
						"SELECT ID,ENTERPRISE_NAME,AVALIABLE_DATE,SIGN_COUNT,CREATE_TIME,NEW_AVALIABLE_DATE,"
						+ " NEW_SIGN_COUNT,SIGNED_COUNT,UPDATE_FLAG,SYSTEM_TYPE,FEE_TYPE,"	
						+ " ALREADY_ACTIVE_TIMES,ACTIVE_TIMES,FORMAL_USE_TIME,VERSION "
						+ " FROM g_enterprise_regist "
						+ " where (ENTERPRISE_NAME is not null) OR (ENTERPRISE_NAME like '%"+key+"%') order by CREATE_TIME desc "
						);
			}
		dataSource.getSelectCommands().add(sql);
		DataTable dt =dataSource.query(0, dic);
		return dt;
	}


	public DataTable queryEnterpriseRegistLogList(String key, Map<String, Object> dic) {
			SqlDataSource dataSource = new SqlDataSource();
		if(StringUtil.isEmptyString(key)){
			key = "";
		}
		Sql sql = new Sql( "SELECT * FROM g_e_regist_log where G_ENTERPRISE_NAME = '"+key+"' order by CREATE_TIME desc ");
		dataSource.getSelectCommands().add(sql);
		return dataSource.query(0, dic);
	}
	
}
